package net.lunjin.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import net.lunjin.bean.Organizations;
import net.lunjin.bean.Remark;
import net.lunjin.bean.RemarkTemplate;
import net.lunjin.dao.OrganizationsDao;
import net.lunjin.dao.RemarkDao;
import net.lunjin.dao.RemarkTemplateDao;
import net.lunjin.service.RemarkService;
import net.lunjin.util.ProjectUtil;

import org.springframework.beans.factory.annotation.Autowired;

import finaler.util.DateUtil;
import finaler.util.Page;
import finaler.util.PageList;

public class RemarkServiceImpl implements RemarkService {

	@Autowired
	private RemarkDao remarkDao;

	@Autowired
	private RemarkTemplateDao remarkTemplateDao;

	@Autowired
	private OrganizationsDao organizationsDao;
	
	@Override
	public PageList getRemarkList(Page page) throws Exception {
		return remarkDao.getRemarkList(page);
	}

	@Override
	public boolean insert(Remark remark) throws Exception {
		return remarkDao.insert(remark);
	}

	@Override
	public Map<String, Object> getAvgFen(BigDecimal orgId) throws Exception {
		return remarkDao.getAvgFen(orgId);
	}

	@Override
	public PageList getGoodOrBadList(Page page) throws Exception {
		return remarkDao.getGoodOrBadList(page);
	}

	@Override
	public void autoRemarkCoin() throws Exception {
		Organizations organizations = organizationsDao.getRandomRecord();//随机获得一个交易平台
		if(organizations==null){return;}
		RemarkTemplate remarkTemplate = remarkTemplateDao.getRandomRecord();//随机获得一个评论模板
		if(remarkTemplate==null){return;}
		Remark remark = new Remark();
		remark.setAutoflag(new BigDecimal(1));
		remark.setCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
		remark.setCreateuser(ProjectUtil.getRandomIP());
		remark.setYcoin(remarkTemplate.getYcoin());
		remark.setDcoin(remarkTemplate.getDcoin());
		remark.setRcoin(remarkTemplate.getRcoin());
		remark.setKcoin(remarkTemplate.getKcoin());
		remark.setOrgid(organizations.getId());
		remark.setRmcontent(remarkTemplate.getRmcontent());
		remarkDao.insert(remark);
	}

	@Override
	public List<Map<String, Object>> getTopRemark(int number) throws Exception {
		return remarkDao.getTopRemark(number);
	}

	@Override
	public void deleteByOrgid(BigDecimal orgId) throws Exception {
		remarkDao.deleteByOrgid(orgId);
	}

	@Override
	public boolean delete(Remark remark) throws Exception {
		return remarkDao.delete(remark);
	}
}
